package com.atom.common.web.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;

import com.atom.common.activiti.HistoricTaskDetailHandler;
import com.atom.common.activiti.diagram.DiagramHandler;
import com.atom.common.activiti.log.LogHandler;
import com.atom.common.dao.CommentDao;
import com.atom.common.entity.CommentDO;
import com.atom.common.entity.bo.TaskDiagramBO;
import com.atom.common.util.JsonUtil;
import com.atom.common.util.ResponseUtil;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.commontools.record.Record;

public abstract class AbstractMainController {
  
  private static Logger logger = Logger.getLogger(AbstractMainController.class);

  @Resource protected CommentDao commentDao;
  /** 查看流程图*/
  @Resource DiagramHandler diagramHandler;
  /** 查看操作日志*/
  @Resource LogHandler taskLogHandler;
  /** 查看已办详细*/
  @Resource HistoricTaskDetailHandler historicTaskDetailHandler;
  
  /**
   * Description: 查看流程图
   * 
   * @param mainProcessInstanceId
   * @return
   */
  public ModelAndView getDiagram(String mainProcessInstanceId,
      String processDefinitionKey,
      String diagramView,
      HttpServletResponse response) {
    boolean isHtml = diagramHandler.isHtml(mainProcessInstanceId);
    if (isHtml) {
      File file;
      try {
        file = diagramHandler.getHtml(mainProcessInstanceId);
        ResponseUtil.responseHtml(response, file);        
      } catch (Exception e) {
        logger.error("error message = {}" + e.getMessage(), e);
      } 
      return null;
    } else {
      Map<String, TaskDiagramBO> data = new HashMap<String, TaskDiagramBO>();
      try {
        data = diagramHandler.getDiagram(mainProcessInstanceId, processDefinitionKey);
      } catch (Exception e) {
        logger.error("error message = {}" + e.getMessage(), e);
      }
      return new ModelAndView(diagramView, "data", JsonUtil.ObjectToJsonQuietly(data));
    }

  }

  /**
   * Description: 查看已办详细
   * 
   * @param taskId
   * @return
   */
  public void getHistory(String taskId,
      HttpServletResponse response) {
    File file;
    try {
      file = historicTaskDetailHandler.getHistory(taskId);
      ResponseUtil.responseHtml(response, file);
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
    } 
  }

  /**
   * Description: 查看操作日志
   * 
   * @param mainProcessInstanceId
   * @return
   */
  public ModelAndView getLog(String mainProcessInstanceId,
      String logView,
      HttpServletResponse response) {
    Map<String, Object> data = new HashMap<String, Object>();
    boolean isHtml = taskLogHandler.isHtml(mainProcessInstanceId);
    if (isHtml) {
      File file;
      try {
        file = taskLogHandler.getHtml(mainProcessInstanceId);
        ResponseUtil.responseHtml(response, file);        
      } catch (Exception e) {
        logger.error("error message = {}" + e.getMessage(), e);
      } 
      return null;
    } else {
      try { 
        data = taskLogHandler.getData(mainProcessInstanceId);
      } catch(Exception e) {
        logger.error("error message = {}" + e.getMessage(), e);
      }
      return new ModelAndView(logView, data);
    }   
  }
  
  public String getCommentByTaskId(String processInstanceId){
	  List<CommentDO> list = commentDao.findByProperty("processInstanceId",processInstanceId);
	  List<Record> rows = new ArrayList<Record>();
	  for(CommentDO d:list){
		  Record r = JsonUtil.ObjectToRecord(d);
		  rows.add(r);
	  }
	  Record result = new Record();
	  result.add(Status.ROWS, rows);
	  result.add(Status.SATUS,Status.OK);
	  return result.toJsonStr();
  }
}
